--[[
  ** 关于生成工具的声明 **

  ** 关于技能的信息 **
--]]
local U = require "packages/utility/utility"
local DIY = require "packages/diy_utility/diy_utility"
local skel = fk.CreateSkill {
  name = "wegame_2__yizhi",
  tags = {},
}

Fk:loadTranslationTable {
  ["wegame_2__yizhi"] = "易帜",
  [":wegame_2__yizhi"] = "当你使用牌后，你可以调离不为此牌目标的一名其他角色至本回合结束。因此进入你攻击范围的角色令你摸一张牌或弃置一张牌。",
  ["#wegame_2__yizhi"] = "易帜：你可以调离不为此牌目标的一名其他角色至本回合结束。",
  ["wegame_2__yizhi_draw"] = "令疾景摸一张牌",
  ["wegame_2__yizhi_discard"] = "令疾景弃置一张牌",
  ["#wegame_2__yizhi-choice"] = "易帜：请选择一项",
}

skel:addEffect(fk.CardUseFinished, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self.name)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local taregts=table.filter(room:getOtherPlayers(player),function (p) return not table.contains(data.tos,p) end)
    local tos = room:askToChoosePlayers(player, { targets = room.alive_players, min_num = 1, max_num = 1, skill_name = skel.name,
    prompt = "#wegame_2__yizhi"})
    if #tos > 0 then
      event:setCostData(self,{tar=tos[1]})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tar
    --先记录攻击范围内的角色
    local old_tars = table.filter(room.alive_players,function (p) return player:inMyAttackRange(p) end)  
    DIY.removePlayer(to, "-turn")
    local new_tars = table.filter(room.alive_players,function (p) return player:inMyAttackRange(p) end)
    
    -- 找到在new_tars中但不在old_tars中的玩家（差集）
    local entered_range_players = {}
    for _, new_player in ipairs(new_tars) do
      local found = false
      for _, old_player in ipairs(old_tars) do
        if new_player == old_player then
          found = true
          break
        end
      end
      if not found then
        table.insert(entered_range_players, new_player)
      end
    end
    if #entered_range_players == 0 then
      return 
    end
    -- 对于新进入攻击范围的玩家，让玩家选择摸一张牌或弃置一张牌
    for _, entered_player in ipairs(entered_range_players) do
      local choice = room:askForChoice(entered_player, {"wegame_2__yizhi_draw", "wegame_2__yizhi_discard"}, 
        skel.name,"#wegame_2__yizhi-choice")
      
      if choice == "wegame_2__yizhi_draw" then
        player:drawCards(1, self.name)
      elseif choice == "wegame_2__yizhi_discard" then
        if player:getHandcardNum() > 0 then
          local cards = room:askForCardChosen(player, player, "h", self.name)
          room:throwCard(cards, self.name, player,player)
        end
      end
    end
  end,
})

return skel
